import asyncio
import datetime
import json
import random
import sys
import aiohttp
import utils

class Book:
    def __init__(self, *args, **kwargs):
        self.title = kwargs.get('title')
        self.author = kwargs.get('author')
        self.isbn = kwargs.get('isbn')
        self.publisher = kwargs.get('publisher')
        self.subject = kwargs.get('subject',None)

    def get_attributes(self):
        return vars(self)

# todo
'''
    爬豆瓣读书等     书籍目录    完成
    爬取畅想之星     书籍目录   完成 10500
    爬取国家图书馆   书籍目录   完成  5000
    爬取qq阅读机构版  书籍目录   完成  12w
    

    搜索接口，遍历所有书籍目录   
        新入库的做处理       注意点：bookStatus设为1
        获取微信读书书籍信息  注意点：id去重、取format:txt(7),epub mobi(1)
        获取书籍章节信息     注意点：只取price为0、—1的章节
        
        
    过滤种子     包含以下类型的  入库时 bookStatus设为 -1
    # category、categories ["艺术-绘画","原版书-英文原版",'原版书','图画','童书-漫画卡通','漫画','工具书','童书','教育学习','绘画','摄影','书法','影视','音乐','舞蹈','视频','艺术']
    # title '漫画'
        
    爬取所有epub书籍  
        注意点：保持登录状态、报错则可能是图片跳过此章、去掉原版书

    爬取部分小说

'''


#数据源 https://www.cxstar.com/Package/List
cxzx_book = utils.mongo_manager(collect_name="cxzx_book", client="mongodb://172.23.130.245:27017/", db='wx_read')
cxzx_book.delete_double()
async def get_books(num, semaphore):
    url = "https://www.cxstar.com/esapi/Package/ClassSearch"
    try:
        async with semaphore:
            async with aiohttp.ClientSession() as session:
                # proxy = utils.get_aioproxy()
                data = {
                    "Page": num,
                    "Size": 15,
                    "type": "1d39470f0028290bce",
                    "pinst": "1ad691ca0000cc0bce",
                    "publisher": "",
                    "SortField": "ORDERNO",
                    "SortType": "ASC",
                    "pubStart": "",
                    "pubEnd": "",
                    "packageId": "2b7cc1bb001fddXXXX"
                }
                headers = {
                    "Accept": "*/*",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Cache-Control": "no-cache",
                    "Connection": "keep-alive",
                    "Content-Type": "application/json",
                    "Origin": "https://www.cxstar.com",
                    "Pragma": "no-cache",
                    "Referer": "https://www.cxstar.com/Package/CloudResource?packageruid=2b7cc1bb001fddXXXX",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin",
                    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest",
                    "sec-ch-ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"",
                    "sec-ch-ua-mobile": "?0",
                    "sec-ch-ua-platform": "\"Linux\""
                }
                cookies = {
                    ".AspNetCore.Antiforgery.t_3ev8QHxHk": "CfDJ8AyqShTIB45PumOSvPKlpZhnwp25SSjxK77PQHFCtkIWgrwGDR9bYZc2lM8lU8jGkQ5BRrwoOoLt93hMwBqAANgN5Hvk6ChfVJbJ58HVHM-82PlGnt3pscZcEIRS3IQ5Dv3ONT5A06SxFPQH0n41VUA",
                    "Hm_lvt_de1fc4dd61dd9ec009c55a5bdb8692b1": "1697435856",
                    "Hm_lpvt_de1fc4dd61dd9ec009c55a5bdb8692b1": "1697446828"
                }
                data = json.dumps(data, separators=(',', ':'))
                async with session.post(url, headers=headers, cookies=cookies, data=data,
                                        proxy=f'http://zheng123:zheng123@wz1.iinti.cn:{random.randint(24000, 24400)}',
                                        proxy_auth=utils.get_aioproxy()) as response:
                    if response.status == 200:
                        text = await response.json()
                        return text['data']
                    else:
                        print(2)
    except BaseException as e:
        print(3)
        print(e)


def parse_data(data):
    return [Book(**i).get_attributes() for i in data]


def save_data(book_data):
    return cxzx_book.insertMany(book_data)


async def main():
    semaphore = asyncio.Semaphore(10)  # 限制并发数为20
    tasks = [get_books(i, semaphore) for i in range(1, 101)]
    # done, pending = await asyncio.wait(tasks)
    # for task in done:
    #     result = task.result()
    #     results.append(result)
    # print(results)
    results = await asyncio.gather(*tasks)
    # print(results)
    book_data=[parse_data(res) for res in results]
    # print(book_data)
    [save_data(data) for data in book_data]



if __name__ == '__main__':
    var = {
        '医学': '1d394a670037e30bce',
        '管理学': '1afcc3c800041d0bce',
        '农学': '1d3947a30029830bce',
        '哲学': '1d3946fb0024f20bce',
        '工学': '1d39451c001cc50bce',
        '艺术学': '1affbedd0041130bce',
        '经济学': '1affbeb0003fcd0bce',
        '法学': '1affbe2b003bd30bce',
        '教育学': '1affbda70039400bce',
        '历史学': '1affbccb0034620bce',
        '理学': '1afcc67e0003960bce',
        '文学': '1affbc3e0000460bce'

    }
    print(start:=datetime.datetime.now())
    asyncio.run(main())
    print(datetime.datetime.now()-start)
